//
//  CertExtension.m
//  TestProject
//
//  Created by 武侠 on 2022/2/17.
//  Copyright © 2022 zhulong. All rights reserved.
//

#import "CertExtension.h"
#import "CertItemLength.h"
#import "CertOID+Extension.h"

@interface CertExtension ()
@end

@implementation CertExtension

/*
 A3 82 01 07(263):
    30 82 01 03(259):
        30 1F(31):
            06 03(03): 55 1D 23     (2.5.29.35)
            04 18(24):
                30 16(22):          (bea67e4d3d7c8fa055e6c12f092e2c2043b3427f)
                    80 14(20): BE A6 7E 4D 3D 7C 8F A0 55 E6 C1 2F 09 2E 2C 20 43 B3 42 7F
 */
// 解析：OID value
- (void)loadOIDValue:(char *)data begin:(int)begin end:(int)end {
    self.type = [CertOID extensionType:self.oid];
    
    // 解析：critical
    int i = begin;
    uint8_t type = data[i];
    if (type == 0x01) {        // critical
        self.critical = [[CertTLV value:data begin:i] isEqual: @"true"];
        CertItemLength *length = [[CertItemLength alloc] initWithData:data index:i+1];
        i += 1+length.lenLen+length.len;
        if (i >= end) {
            return;
        }
        type = data[i];
    }
    
    // 解析：value 04

    CertItemLength *length = [[CertItemLength alloc] initWithData:data index:i+1];
    i += 1 + length.lenLen;     // 偏移0x04的长度
    if (i >= end) {
        return;
    }
    
    // 计算value的长度
    length = [[CertItemLength alloc] initWithData:data index:i+1];
    NSData *valueData = [NSData dataWithBytes:data+i length:1+length.lenLen+length.len];
    self.evalue = [CertExtension extensionValue:valueData type:self.type];
}

+ (_BaseCert *)extensionValue:(NSData *)data type:(CertExtensionType)type {
    switch (type) {
        case AuthorityKeyIdentifier:    // 机构密钥标识符
            return [[CertKeyIdentifier alloc] initWithData:data];
        case KeyUsage:                  // 密钥用法(双证书为关键，单证书为非关键)
            return [[CertKeyUsage alloc] initWithData:data];
        case BasicConstraints:          // 基本限制
            return [[CertBasicConstraints alloc] initWithData:data];
        case CertificatePolicies:       // CP 证书策略 2.5.29.32
            return [[CertPolicies alloc] initWithData:data];
        case SubjectKeyIdentifier:      // 主体密钥标识符
            return [[CertExtensionValue alloc] initWithData:data];
        case CRLDistributionPoints:     // CRL分发点
        case FreshestCRL:               // 最新的CRL
            return [[CertDistributionPoints alloc] initWithData:data];
        case ExtKeyUsage:               // 扩展密钥用途
            return [[CertExtendedKeyUsage alloc] initWithData:data];
        case ExtCertNetscapeType:       // Netscape证书类型
            return [[CertNetscapeType alloc] initWithData:data];
        case AuthorityInfoAccess:       // 颁发机构信息访问
            return [[CertAuthorityInfoAccess alloc] initWithData:data];
        case SubjectAltName:            // 使用者备用名称
        case IssuerAltName:             // 颁发者备用名称
            return [[CertGeneralName alloc] initWithData:data];
        /* 自定义扩展 */
        case Extension_Person_Identify_Code:    // 个人身份标识码
            return [[CertPersonIdentifyCode alloc] initWithData:data];
        case Extension_Insurance_Number:        // 个人社会保险号码
        case Extension_ICRegistration_Number:   // 企业工商注册号码
        case Extension_Organization_Code:       // 企业组织机构代码
        case Extension_Taxation_Numeber:        // 企业税号
            return [[CertCardCode alloc] initWithData:data];
        case Extension_Entity_Unique_ID:        // 证书实体唯一标识
            return [[CertExtensionValue alloc] initWithData:data];
        /* 北京CA扩展 */
        case Extension_BJCA_IDCard:             // 北京CA-个人身份证
        case Extension_BJCA_EnterpriseCode:     // 北京CA-企业机构代码
        case Extension_BJCA_NONE_1:             // 北京CA-未知扩展1
        case Extension_BJCA_NONE_14:            // 北京CA-未知扩展14
        case Extension_BJCA_NONE_30:            // 北京CA-未知扩展30
        case Extension_BJCA_NONE_3_2:           // 北京CA-未知扩展3.2
            return [[CertExtensionValue alloc] initWithData:data];
            
        default:
            return [[CertExtensionValue alloc] initWithData:data];
//            return [[_BaseCert alloc] initWithData:data];
    }
}

- (NSString *)description {
    if ([self.evalue.description hasPrefix:@"[\n"] == NO && [self.evalue.description hasPrefix:@"{\n"] == NO) {
        return [NSString stringWithFormat:@"%@(%@) %@ \n\t%@", self.oid, self.oidDesc, self.critical == true ? @"关键" : @"非关键", self.evalue.description];
    } else {
        return [NSString stringWithFormat:@"%@(%@) %@ \n%@", self.oid, self.oidDesc, self.critical == true ? @"关键" : @"非关键", self.evalue.description];
    }
}

@end

/*
 30 19
    06 03 55 1D 11
    04 12
        30 10(16):
            82 0E(14): 77 77 77 2E 67 6F 6F 67 6C 65 2E 63 6F 6D
 */
